#!/bin/bash

# 快速创建Maxwell配置文件脚本

echo "=== 创建Maxwell配置文件 ==="
echo "时间: $(date)"
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 默认配置
DEFAULT_MAXWELL_HOME="/opt/module/maxwell"
DEFAULT_MYSQL_HOST="192.168.10.102"
DEFAULT_MYSQL_PORT="3306"
DEFAULT_MYSQL_USER="maxwell"
DEFAULT_MYSQL_PASSWORD="maxwell"
DEFAULT_KAFKA_SERVERS="hadoop102:9092,hadoop103:9092,hadoop104:9092"
DEFAULT_KAFKA_TOPIC="maxwell"

# 获取用户输入
echo -e "${BLUE}请输入配置信息（直接回车使用默认值）:${NC}"
echo ""

read -p "Maxwell安装路径 [$DEFAULT_MAXWELL_HOME]: " MAXWELL_HOME
MAXWELL_HOME=${MAXWELL_HOME:-$DEFAULT_MAXWELL_HOME}

read -p "MySQL主机地址 [$DEFAULT_MYSQL_HOST]: " MYSQL_HOST
MYSQL_HOST=${MYSQL_HOST:-$DEFAULT_MYSQL_HOST}

read -p "MySQL端口 [$DEFAULT_MYSQL_PORT]: " MYSQL_PORT
MYSQL_PORT=${MYSQL_PORT:-$DEFAULT_MYSQL_PORT}

read -p "MySQL用户名 [$DEFAULT_MYSQL_USER]: " MYSQL_USER
MYSQL_USER=${MYSQL_USER:-$DEFAULT_MYSQL_USER}

read -p "MySQL密码 [$DEFAULT_MYSQL_PASSWORD]: " MYSQL_PASSWORD
MYSQL_PASSWORD=${MYSQL_PASSWORD:-$DEFAULT_MYSQL_PASSWORD}

read -p "Kafka服务器 [$DEFAULT_KAFKA_SERVERS]: " KAFKA_SERVERS
KAFKA_SERVERS=${KAFKA_SERVERS:-$DEFAULT_KAFKA_SERVERS}

read -p "Kafka主题 [$DEFAULT_KAFKA_TOPIC]: " KAFKA_TOPIC
KAFKA_TOPIC=${KAFKA_TOPIC:-$DEFAULT_KAFKA_TOPIC}

# 配置文件路径
CONFIG_FILE="$MAXWELL_HOME/config.properties"

echo ""
echo -e "${BLUE}配置信息确认:${NC}"
echo "  Maxwell路径: $MAXWELL_HOME"
echo "  MySQL主机: $MYSQL_HOST:$MYSQL_PORT"
echo "  MySQL用户: $MYSQL_USER"
echo "  Kafka服务器: $KAFKA_SERVERS"
echo "  Kafka主题: $KAFKA_TOPIC"
echo "  配置文件: $CONFIG_FILE"
echo ""

read -p "确认创建配置文件？(y/N): " CONFIRM
if [[ ! $CONFIRM =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}取消创建配置文件${NC}"
    exit 0
fi

# 创建目录
echo "创建Maxwell目录..."
mkdir -p "$MAXWELL_HOME"
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 目录创建成功${NC}"
else
    echo -e "${RED}✗ 目录创建失败${NC}"
    exit 1
fi

# 创建配置文件
echo "创建配置文件..."
cat > "$CONFIG_FILE" << EOF
# Maxwell配置文件
# 创建时间: $(date)

# MySQL连接配置
log_level=info

# MySQL主库连接信息
host=$MYSQL_HOST
port=$MYSQL_PORT
user=$MYSQL_USER
password=$MYSQL_PASSWORD

# Maxwell数据库配置
schema_database=maxwell

# 输出配置 - Kafka
producer=kafka
kafka.bootstrap.servers=$KAFKA_SERVERS

# Kafka主题配置
kafka_topic=$KAFKA_TOPIC

# 输出格式
output_type=json

# 过滤配置（可选）
# filter=exclude:*.*,include:test.*,include:db1.table1

# 初始化位置配置
init_position=auto

# 心跳配置
heartbeat_interval=30

# 连接池配置
connection_pool_size=10

# 批处理配置
output_batch_size=100
output_batch_timeout_ms=1000

# 压缩配置
compression_type=gzip

# 监控配置
metrics_type=http
metrics_port=8080

# 自定义配置
custom_producer_properties=compression.type=gzip,retries=3

# 输出到文件（可选，用于调试）
# producer=file
# output_file=/tmp/maxwell_output.json

# 输出到stdout（可选，用于调试）
# producer=stdout
EOF

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 配置文件创建成功${NC}"
else
    echo -e "${RED}✗ 配置文件创建失败${NC}"
    exit 1
fi

# 设置权限
echo "设置文件权限..."
chmod 644 "$CONFIG_FILE"
chown $USER:$USER "$CONFIG_FILE"

# 验证配置文件
echo "验证配置文件..."
if [ -f "$CONFIG_FILE" ]; then
    echo -e "${GREEN}✓ 配置文件存在${NC}"
    echo "配置文件内容预览:"
    echo "----------------------------------------"
    head -20 "$CONFIG_FILE"
    echo "----------------------------------------"
else
    echo -e "${RED}✗ 配置文件不存在${NC}"
    exit 1
fi

# 创建日志目录
echo "创建日志目录..."
mkdir -p "$MAXWELL_HOME/logs"
chmod 755 "$MAXWELL_HOME/logs"

# 创建启动脚本
echo "创建启动脚本..."
cat > "$MAXWELL_HOME/start_maxwell.sh" << 'EOF'
#!/bin/bash
# Maxwell启动脚本

MAXWELL_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="$MAXWELL_HOME/config.properties"
LOG_FILE="$MAXWELL_HOME/logs/maxwell.log"
PID_FILE="$MAXWELL_HOME/maxwell.pid"

echo "启动Maxwell..."
echo "配置文件: $CONFIG_FILE"
echo "日志文件: $LOG_FILE"

# 检查配置文件
if [ ! -f "$CONFIG_FILE" ]; then
    echo "错误: 配置文件不存在: $CONFIG_FILE"
    exit 1
fi

# 启动Maxwell
nohup $MAXWELL_HOME/bin/maxwell --config="$CONFIG_FILE" > "$LOG_FILE" 2>&1 &
MAXWELL_PID=$!

# 保存PID
echo $MAXWELL_PID > "$PID_FILE"

echo "Maxwell已启动 (PID: $MAXWELL_PID)"
echo "查看日志: tail -f $LOG_FILE"
echo "停止服务: kill $MAXWELL_PID"
EOF

chmod +x "$MAXWELL_HOME/start_maxwell.sh"

# 创建停止脚本
echo "创建停止脚本..."
cat > "$MAXWELL_HOME/stop_maxwell.sh" << 'EOF'
#!/bin/bash
# Maxwell停止脚本

MAXWELL_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_FILE="$MAXWELL_HOME/maxwell.pid"

echo "停止Maxwell..."

if [ -f "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if kill -0 $PID 2>/dev/null; then
        echo "停止进程 (PID: $PID)..."
        kill $PID
        sleep 2
        
        if kill -0 $PID 2>/dev/null; then
            echo "强制停止进程..."
            kill -9 $PID
        fi
        
        rm -f "$PID_FILE"
        echo "Maxwell已停止"
    else
        echo "进程不存在 (PID: $PID)"
        rm -f "$PID_FILE"
    fi
else
    echo "未找到PID文件"
    PIDS=$(pgrep -f "maxwell")
    if [ -n "$PIDS" ]; then
        echo "找到Maxwell进程: $PIDS"
        kill $PIDS
        echo "Maxwell已停止"
    else
        echo "未找到运行中的Maxwell进程"
    fi
fi
EOF

chmod +x "$MAXWELL_HOME/stop_maxwell.sh"

# 创建状态检查脚本
echo "创建状态检查脚本..."
cat > "$MAXWELL_HOME/status_maxwell.sh" << 'EOF'
#!/bin/bash
# Maxwell状态检查脚本

MAXWELL_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_FILE="$MAXWELL_HOME/maxwell.pid"

echo "检查Maxwell状态..."

if [ -f "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if kill -0 $PID 2>/dev/null; then
        echo "✓ Maxwell正在运行 (PID: $PID)"
        ps -p $PID -o pid,ppid,cmd,etime,pcpu,pmem
        return 0
    else
        echo "✗ Maxwell进程不存在 (PID: $PID)"
        rm -f "$PID_FILE"
        return 1
    fi
else
    PIDS=$(pgrep -f "maxwell")
    if [ -n "$PIDS" ]; then
        echo "✓ 找到Maxwell进程: $PIDS"
        return 0
    else
        echo "✗ Maxwell未运行"
        return 1
    fi
fi
EOF

chmod +x "$MAXWELL_HOME/status_maxwell.sh"

echo ""
echo -e "${GREEN}=== 配置文件创建完成 ===${NC}"
echo ""
echo "文件位置:"
echo "  配置文件: $CONFIG_FILE"
echo "  启动脚本: $MAXWELL_HOME/start_maxwell.sh"
echo "  停止脚本: $MAXWELL_HOME/stop_maxwell.sh"
echo "  状态脚本: $MAXWELL_HOME/status_maxwell.sh"
echo "  日志目录: $MAXWELL_HOME/logs/"
echo ""
echo "使用方法:"
echo "  编辑配置: vi $CONFIG_FILE"
echo "  启动服务: $MAXWELL_HOME/start_maxwell.sh"
echo "  停止服务: $MAXWELL_HOME/stop_maxwell.sh"
echo "  查看状态: $MAXWELL_HOME/status_maxwell.sh"
echo "  查看日志: tail -f $MAXWELL_HOME/logs/maxwell.log"
echo ""
echo -e "${BLUE}下一步操作:${NC}"
echo "1. 编辑配置文件: vi $CONFIG_FILE"
echo "2. 测试MySQL连接: mysql -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e 'SELECT 1;'"
echo "3. 启动Maxwell: $MAXWELL_HOME/start_maxwell.sh" 